import { STORE_ADD_HIVE_KEY, STORE_CLEAR_HIVE_KEY } from '../actions/hive_key';
import { Map as ImmutableMap } from 'immutable';

const initialState = () => ImmutableMap({
  owner:localStorage.getItem('owner'),
  active:localStorage.getItem('active'),
  posting:localStorage.getItem('posting'),
  memo:localStorage.getItem('memo'),
});

export function hiveKeyReducer(state = initialState(), action) {
  switch(action.type) {
  case STORE_ADD_HIVE_KEY:
    Object.keys(action.value).map(key=>{
      localStorage.setItem(key, action.value[key]);
    });
    console.log(localStorage.getItem('posting'));
    return state.merge(action.value);
    case STORE_CLEAR_HIVE_KEY:
    state.mapKeys(key=>{
      localStorage.removeItem(key);
    });
    ;
    return initialState();
  default:
    return state;
  }
}
